package com.itheima.web.controller.sms;


import com.itheima.result.PageResult;
import com.itheima.result.ResultData;
import com.itheima.service.sms.CouponService;
import com.itheima.vo.sms.Coupon;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("coupon")
@Slf4j
public class CouponController {

    @DubboReference
    private CouponService couponService;

    /**
     * 添加优惠券
     * @param couponParam
     * @return
     */
    @PostMapping("create")
    public ResultData create(@RequestBody Coupon couponParam){
        log.info("添加的优惠卷信息：{}",couponParam);
        int result=couponService.create(couponParam);
        return ResultData.ok("操作成功",result);
    }

    /**
     * 删除优惠券
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public ResultData delete(@PathVariable Integer id){
        log.info("删除的优惠卷id：{}",id);
        int result=couponService.deleteById(id);
        return ResultData.ok("操作成功",result);
    }

    /**
     * 根据优惠券名称和类型分页获取优惠券列表
     * @return
     */
    @GetMapping("list")
    public ResultData list(String name,Integer pageNum,Integer pageSize,Integer type){
        log.info("分页查询的信息：{}，{}，{}，{}",name,pageNum,pageSize,type);
        PageResult pageResult=couponService.select(name,pageNum,pageSize,type);
        return ResultData.ok("操作成功",pageResult);
    }

    /**
     * 修改优惠券
     * @param id
     * @param couponParam
     * @return
     */
    @PostMapping("/update/{id}")
    public ResultData update(@PathVariable Long id,@RequestBody Coupon couponParam){
        int result=couponService.updateCoupon(id,couponParam);
        return ResultData.ok("操作成功",result);
    }

    /**
     * 获取单个优惠券的详细信息
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public ResultData selectById(@PathVariable Long id){
        Coupon coupon=couponService.selectById(id);
        return ResultData.ok("操作成功",coupon);
    }
}
